package com.photographysharing.controller;


import com.photographysharing.common.ThreadLocalOperation;
import com.photographysharing.dto.MenusDTO;
import com.photographysharing.entity.Menus;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.request.SelectByPaging;
import com.photographysharing.response.DatasResponse;
import com.photographysharing.response.MessageResponse;
import com.photographysharing.service.MenusService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menu")
public class MenuControler {
    private MenusService menusService;

    public MenuControler(MenusService menusService) {
        this.menusService = menusService;
    }

    @PostMapping("/add")
    public MessageResponse add(@RequestBody Menus menu){
        if(menusService.save(menu)){
            return new MessageResponse("增加成功!");
        }
        return new MessageResponse("增加失败",false);
    }

    @GetMapping("/select")
    public DatasResponse<MenusDTO> select(SelectByPaging paging){
        return  menusService.select(paging);
    }

    @GetMapping("/getMenus")
    public DatasResponse<MenusDTO> getMenus(){
        String role = ThreadLocalOperation.getThreadLocaclRole();
        return menusService.getMenus(role);
    }

    @PostMapping("/update")
    public MessageResponse update(@RequestBody Menus menu){
        if(menusService.updateById(menu)){
            return  new MessageResponse("更新成功!");
        }
        return new MessageResponse("更新失败!",false);
    }

    @DeleteMapping("/delete")
    public  MessageResponse delete(Integer id){
        if(menusService.removeById(id)){
            return  new MessageResponse("删除成功!");
        }
        return new MessageResponse("删除失败!",false);
    }
}
